﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;

namespace Akso.Authorization
{
    /// <summary>
    /// Application's authorization provider.
    /// Defines permissions for the application.
    /// See <see cref="AppPermissions"/> for all permission names.
    /// </summary>
    public class AppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            //COMMON PERMISSIONS (FOR BOTH OF TENANTS AND HOST)

            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"));

            //Basci Data Permissions

            var basicData = pages.CreateChildPermission(AppPermissions.Pages_BasicData, L("Menu_BasicData"));

            var vendors = basicData.CreateChildPermission(AppPermissions.Pages_BasicData_Vendors, L("Menu_BasicData.Vendors"));
            vendors.CreateChildPermission(AppPermissions.Pages_BasicData_Vendors_Create, L("Permission_BasicData.Vendors_Create"));
            vendors.CreateChildPermission(AppPermissions.Pages_BasicData_Vendors_Edit, L("Permission_BasicData.Vendors_Edit"));
            vendors.CreateChildPermission(AppPermissions.Pages_BasicData_Vendors_Delete, L("Permission_BasicData.Vendors_Delete"));

            //Inventory Data
            var inventory = pages.CreateChildPermission(AppPermissions.Pages_Inventory, L("Menu_Inventory"));

            //进货单权限
            var inbounds = inventory.CreateChildPermission(AppPermissions.Pages_Inventory_Inbounds, L("Menu_Inventory.Inbounds"));
            inbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Inbounds_Create, L("Permission_Inventory.Inbounds_Create"));
            inbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Inbounds_Edit, L("Permission_Inventory.Inbounds_Edit"));
            inbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Inbounds_Delete, L("Permission_Inventory.Inbounds_Delete"));
            inbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Inbounds_Import, L("Permission_Inventory.Inbounds_Import"));

            //出库单权限
            var outbounds = inventory.CreateChildPermission(AppPermissions.Pages_Inventory_Outbounds, L("Menu_Inventory.Outbounds"));
            outbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Outbounds_Create, L("Permission_Inventory.Outbounds_Create"));
            outbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Outbounds_Edit, L("Permission_Inventory.Outbounds_Edit"));
            outbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Outbounds_Delete, L("Permission_Inventory.Outbounds_Delete"));
            outbounds.CreateChildPermission(AppPermissions.Pages_Inventory_Outbounds_Import, L("Permission_Inventory.Outbounds_Import"));
            //Management Permissions
            var administration = pages.CreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));

            var roles = administration.CreateChildPermission(AppPermissions.Pages_Administration_Roles, L("Roles"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Create, L("CreatingNewRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Edit, L("EditingRole"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Delete, L("DeletingRole"));

            var users = administration.CreateChildPermission(AppPermissions.Pages_Administration_Users, L("Users"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Create, L("CreatingNewUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Edit, L("EditingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Delete, L("DeletingUser"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_ChangePermissions, L("ChangingPermissions"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Impersonation, L("LoginForUsers"));

            var languages = administration.CreateChildPermission(AppPermissions.Pages_Administration_Languages, L("Languages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Create, L("CreatingNewLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Edit, L("EditingLanguage"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_Delete, L("DeletingLanguages"));
            languages.CreateChildPermission(AppPermissions.Pages_Administration_Languages_ChangeTexts, L("ChangingTexts"));

            administration.CreateChildPermission(AppPermissions.Pages_Administration_AuditLogs, L("AuditLogs"));

            var organizationUnits = administration.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits, L("OrganizationUnits"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageOrganizationTree, L("ManagingOrganizationTree"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_ManageMembers, L("ManagingMembers"));


            //TENANT-SPECIFIC PERMISSIONS

            pages.CreateChildPermission(AppPermissions.Pages_Tenant_Dashboard, L("Dashboard"), multiTenancySides: MultiTenancySides.Tenant);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Tenant_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Tenant);

            //HOST-SPECIFIC PERMISSIONS

            var editions = pages.CreateChildPermission(AppPermissions.Pages_Editions, L("Editions"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Create, L("CreatingNewEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Edit, L("EditingEdition"), multiTenancySides: MultiTenancySides.Host);
            editions.CreateChildPermission(AppPermissions.Pages_Editions_Delete, L("DeletingEdition"), multiTenancySides: MultiTenancySides.Host);

            var tenants = pages.CreateChildPermission(AppPermissions.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Create, L("CreatingNewTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Edit, L("EditingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_ChangeFeatures, L("ChangingFeatures"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Delete, L("DeletingTenant"), multiTenancySides: MultiTenancySides.Host);
            tenants.CreateChildPermission(AppPermissions.Pages_Tenants_Impersonation, L("LoginForTenants"), multiTenancySides: MultiTenancySides.Host);

            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Settings, L("Settings"), multiTenancySides: MultiTenancySides.Host);
            administration.CreateChildPermission(AppPermissions.Pages_Administration_Host_Maintenance, L("Maintenance"), multiTenancySides: MultiTenancySides.Host);
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AbpZeroTemplateConsts.LocalizationSourceName);
        }
    }
}
